﻿using System;
using System.Collections.Generic;
using System.Linq;
using ConsoleAppFramework.Util;
using ConsoleAppFramework.Rmq;

namespace ConsoleApp.Rmq
{
    public class RmqQueueManager
    {
        #region 队列定义
        public static class QueueName
        {
            public static string 线上飞书消息
            {
                get
                {
                   return "Shusi_Online_FeiShuMsg_Beta";
                   
                }
            }

            public static string 测试
            {
                get
                {
                    return "Shusi_Backend_Test_Beta";
                }
            }

            public static string Canal消息
            {
                get
                {
                    return "canal";
                }
            }
        }
       
        #endregion

        private const string BasicRouteKey = "Xigua.Shusi.";

        private const string Exchange = "Xigua.Shusi";

        private static RmqHttpApiService _service = new RmqHttpApiService(RmqUrlEnum.XiGuaWx);
       
        public static void Enqueue<T>(T messageDTO, string queueName, int? expiration = null) where T : class
        {
            RmqMessageProducer.MessageSend<T>(RmqUrlEnum.XiGuaWx, Exchange, BasicRouteKey + queueName, messageDTO, new RmqMessageProps()
            {
                DeliveryMode = 1,
                Expiration = expiration
            });
        }

        public static void Enqueue<T>(T messageDTO, string routeKey, RmqMessagePriorityLevel priorityLevel, int? expiration = null) where T : class
        {
            RmqMessageProducer.MessageSend<T>(RmqUrlEnum.XiGuaWx, Exchange, BasicRouteKey + routeKey, messageDTO, new RmqMessageProps()
            {
                DeliveryMode = 1,
                Priority = priorityLevel,
                Expiration = expiration
            });
        }

        public static void EnqueueFromList<T>(List<T> messageDTOList, string queueName, int pageSize = 30, int? expiration = null) where T : class
        {
            if (messageDTOList.Count > 0)
            {
                var pageIndex = 1;
                while ((pageIndex - 1) * pageSize < messageDTOList.Count)
                {
                    try
                    {
                        var temp = messageDTOList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        RmqMessageProducer.MessageSend<List<T>>(RmqUrlEnum.XiGuaWx, Exchange, BasicRouteKey + queueName, temp, new RmqMessageProps()
                        {
                            DeliveryMode = 1,
                            Expiration = expiration
                        });

                        pageIndex++;
                    }
                    catch (Exception ex)
                    {
                        ConsoleHelper.WriteErrorLine(ex.ToString());
                    }
                }
            }
        }

        /// <summary>
        /// 获取队列消息总数
        /// </summary>
        /// <param name="queueName"></param>
        /// <returns></returns>
        public static int GetQueueCount(string queueName)
        {
            return _service.GetMessageCount(queueName);
        }

        /// <summary>
        /// 获取节点信息
        /// </summary>
        /// <returns></returns>
        public static string GetNodesDetail()
        {
            return _service.GetNodesDetail();
        }
    }
}
